package com.intramirror.product.utils;

import java.util.Calendar;
import java.util.Date;

/**
 * Created on 2018/5/8.
 * @author 123
 */
public class SeasonUtil {

    public static void main(String[] argc) {
        carryOverToNewSeasonCode();
    }

    public static String carryOverToNewSeasonCode() {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());

        StringBuilder season = new StringBuilder();
        season.append(c.get(Calendar.YEAR) % 100);
        int month = c.get(Calendar.MONTH) + 1;
        if (month > 6) {
            season.append("FW");
        } else {
            season.append("SS");
        }

        return season.toString();
    }

    public static String getNewerSeason(String season1, String season2) {
        if (season1 == null || season2 == null) {
            return season1 == null ? season2 : season1;
        }

        String seasonA = season1.toUpperCase();
        String seasonB = season2.toUpperCase();
        if ("CarryOver".equalsIgnoreCase(season1)) {
            seasonA = carryOverToNewSeasonCode();
        }

        if ("CarryOver".equalsIgnoreCase(season2)) {
            seasonB = carryOverToNewSeasonCode();
        }

        seasonA = seasonA.replace("FW", "02").replace("SS", "01");
        seasonB = seasonB.replace("FW", "02").replace("SS", "01");

        if ("CLOSEOUT".equals(seasonA.toUpperCase())) {
            return season2;
        }

        if ("CLOSEOUT".equals(seasonB.toUpperCase())) {
            return season1;
        }

        if (Integer.valueOf(seasonA) > Integer.valueOf(seasonB)) {
            return season1;
        } else {
            return season2;
        }

    }

    public static boolean seasonEqual(String season1, String season2) {
        if (season1 == null || season2 == null) {
            return false;
        }

        String seasonA = season1;
        String seasonB = season2;
        if ("CarryOver".equalsIgnoreCase(season1)) {
            seasonA = carryOverToNewSeasonCode();
        }

        if ("CarryOver".equalsIgnoreCase(season2)) {
            seasonB = carryOverToNewSeasonCode();
        }

        return seasonA.equalsIgnoreCase(seasonB);
    }

}
